# packages
library(here)
library(tidyverse)
library(splitstackshape)
library(dplyr)
library(ggpattern)
library(forcats)
library(estimatr)
library(texreg)
library(splitstackshape)
library(tm)
library(wordcloud2)
library(ggplot2)
library(topicmodels)
library(MASS)
library(robust)
library(sandwich)
library(mediation)
library(sjPlot)

# load data
survey_clean <- read.csv("survey_clean.csv")

survey_clean <- survey_clean %>%
  mutate(
    forced2 = ifelse(forced == 2, 1, NA),
    forced2 = ifelse(forced == 1, 0, forced2),
    attack2 = ifelse(attack == 0, 2, attack),
    attack2 = ifelse(is.na(attack), 0, attack),
    religion2 = ifelse(religion == "I am not religious", 0, 1)
  )

survey_clean$ad_align <- as.factor(survey_clean$ad_align)
survey_clean$align <- as.factor(survey_clean$align)
survey_clean$religion2 <- as.factor(survey_clean$religion2)

survey_clean2 <- survey_clean %>%
  filter(!is.na(party2))

survey_clean2 <- survey_clean2 %>%
  mutate(
    strong_dem = ifelse(party2 == 1, 2, 0),
    strong_dem = ifelse(party2 == 2, 1, strong_dem),
    strong_rep = ifelse(party2 == 5, 2, 0),
    strong_rep = ifelse(party2 == 4, 1, strong_rep),
    strong_partisan2 = ifelse(party2 == 1 | party2 == 5, 2, 0),
    strong_partisan2 = ifelse(party2 == 2 | party2 == 4, 1, strong_partisan2),
    strong_partisan2 = ifelse(party2 == 3, 0, strong_partisan2),
  )

survey_clean2$strong_partisan2 <- as.factor(survey_clean2$strong_partisan2)
survey_clean2$strong_dem <- as.factor(survey_clean2$strong_dem)
survey_clean2$strong_rep <- as.factor(survey_clean2$strong_rep)
survey_clean2$align <- as.factor(survey_clean2$align)
survey_clean2$ad_align <- as.factor(survey_clean2$ad_align)

survey_clean2$oppose <- rev(survey_clean2$feel)

## Hypothesis 4a: Strong partisans are more likely to experience heightened and more intense feelings of support toward Supreme Court nominees when those nominees align with their political party's ideology and preferences. 

h4a_model_action_support_noctrl <- lm_robust(feel ~ strong_partisan*align*ad_align + forced + recognize + attack2 +  length, data = survey_clean2, cluster = survey_clean2$ResponseId)

h4a_model_action_support <- lm_robust(feel ~ strong_partisan*align*ad_align + forced + recognize + attack2 +  length + legit_mean + female + age_range + minority + edu + income + religion + donate_groups + call_confirm_past + region + state_length + know + attn, data = survey_clean2, cluster = survey_clean2$ResponseId)

texreg(list(h4a_model_action_support_noctrl, h4a_model_action_support))

## Hypothesis 4ai: Strong Democratic partisans are more likely to experience heightened and more intense feelings of support toward Supreme Court nominees when those nominees align with their political party's ideology and preferences. 

h4ai_model_action_support_noctrl <- lm_robust(feel ~ strong_dem*align*ad_align + forced + recognize + attack2 +  length, data = survey_clean2, cluster = survey_clean2$ResponseId)

h4ai_model_action_support <- lm_robust(feel ~ strong_dem*align*ad_align + forced + recognize + attack2 +  length + legit_mean + female + age_range + minority + edu + income + religion + donate_groups + call_confirm_past + region + state_length + know + attn, data = survey_clean2, cluster = survey_clean2$ResponseId)

texreg(list(h4ai_model_action_support_noctrl, h4ai_model_action_support))

## Hypothesis 4aii: Strong Republican partisans are more likely to experience heightened and more intense feelings of support toward Supreme Court nominees when those nominees align with their political party's ideology and preferences. 

h4aii_model_action_support_noctrl <- lm_robust(feel ~ strong_rep*align*ad_align + forced + recognize + attack2 +  length, data = survey_clean2, cluster = survey_clean2$ResponseId)

h4aii_model_action_support <- lm_robust(feel ~ strong_rep*align*ad_align + forced + recognize + attack2 +  length + legit_mean + female + age_range + minority + edu + income + religion + donate_groups + call_confirm_past + region + state_length + know + attn, data = survey_clean2, cluster = survey_clean2$ResponseId)

texreg(list(h4aii_model_action_support_noctrl, h4aii_model_action_support))

## Hypothesis 4b: Strong partisans are more likely to experience heightened and more intense feelings of opposition toward Supreme Court nominees when those nominees contradict their political party's ideology and preferences. 

h4b_model_action_oppose_noctrl <- lm_robust(oppose ~ strong_partisan*contradict*ad_align, data = survey_clean2, cluster = survey_clean2$ResponseId)

h4b_model_action_oppose <- lm_robust(oppose ~ strong_partisan*contradict*ad_align + forced + recognize + attack2 +  length + legit_mean + female + age_range + minority + edu + income + religion + religion_frequency + donate_groups + call_confirm_past + region + state_length + know + attn, data = survey_clean2, cluster = survey_clean2$ResponseId)

screenreg(list(h4b_model_action_oppose_noctrl, h4b_model_action_oppose))

## Hypothesis 4bi: Strong Democratic partisans are more likely to experience heightened and more intense feelings of opposition toward Supreme Court nominees when those nominees contradict their political party's ideology and preferences.

h4bi_model_action_oppose_noctrl <- lm_robust(oppose ~ strong_dem*contradict*ad_align, data = survey_clean2, cluster = survey_clean2$ResponseId)

h4bi_model_action_oppose <- lm_robust(oppose ~ strong_dem*contradict*ad_align + forced + recognize + attack2 + length + legit_mean + female + age_range + minority + edu + income + religion + religion_frequency + donate_groups + call_confirm_past + region + state_length + know + attn, data = survey_clean2, cluster = survey_clean2$ResponseId)

screenreg(list(h4bi_model_action_oppose_noctrl, h4bi_model_action_oppose))

## Hypothesis 4bii: Strong Republican partisans are more likely to experience heightened and more intense feelings of opposition toward Supreme Court nominees when those nominees contradict their political party's ideology and preferences. 

h4bii_model_action_oppose_noctrl <- lm_robust(oppose ~ strong_rep*contradict*ad_align, data = survey_clean2, cluster = survey_clean2$ResponseId)

h4bii_model_action_oppose <- lm_robust(oppose ~ strong_rep*contradict*ad_align + forced + recognize + attack2 + length + legit_mean + female + age_range + minority + edu + income + religion + religion_frequency + donate_groups + call_confirm_past + region + state_length + know + attn, data = survey_clean2, cluster = survey_clean2$ResponseId)

screenreg(list(h4bii_model_action_oppose_noctrl, h4bii_model_action_oppose))

# graphics

## fig 2
graph4a <- plot_predictions(h4a_model_action_support, condition = c("strong_partisan", "align", "ad_align"), draw = FALSE)

partisan_names <- c(
  `0` = "Respondent Not Strong Partisan",
  `1` = "Respondent Strong Partisan"
)

align_names <- c(
  `0` = "Nominee Not Aligned",
  `1` = "Nominee Aligned"
)

ad_names <- c(
  `0` = "No Ad",
  `1` = "Co-Partisan Ad",
  `2` = "Not Co-Partisan Ad"
)

ggplot(data = graph4a, aes(x = align, y = estimate, shape = strong_partisan)) +
  geom_point(position = position_dodge(width = 0.8), alpha = 0.5, size = 3) +
  geom_errorbar(
    aes(ymin = estimate - 1.96*std.error, ymax = estimate + 1.96*std.error),
    position = position_dodge(width = 0.8), color = "black", width = 0.1
  ) +
  theme_bw() +
  ylab("Support") +
  xlab("") +
  theme(
    legend.position = "bottom"
  ) + 
  scale_x_discrete(breaks = c(0, 1), labels = c("Not Co-Partisan\nNominee", "Co-Partisan\nNominee")) +
  facet_grid(~ ad_align, labeller = labeller(ad_align = ad_names)) +
  scale_shape_manual(name = "Partisanship", labels = c("Not Strong Partisan", "Strong Partisan"), values = c(15, 17))

## fig 3

graph4ai <- plot_predictions(h4ai_model_action_support, condition = c("strong_dem", "align", "ad_align"), draw = FALSE)


ggplot(data = graph4ai, aes(x = align, y = estimate, shape = strong_dem)) +
  geom_point(position = position_dodge(width = 0.8), alpha = 0.5, size = 3) +
  geom_errorbar(
    aes(ymin = estimate - 1.96*std.error, ymax = estimate + 1.96*std.error),
    position = position_dodge(width = 0.8), color = "black", width = 0.1
  ) +
  theme_bw() +
  ylab("Support") +
  xlab("") +
  theme(
    legend.position = "bottom"
  ) + 
  scale_x_discrete(breaks = c(0, 1), labels = c("Not Co-Partisan\nNominee", "Co-Partisan\nNominee")) +
  facet_grid(~ ad_align, labeller = labeller(ad_align = ad_names)) +
  scale_shape_manual(name = "Partisanship", labels = c("Not Dem.", "Weak Dem.", "Strong Dem."), values = c(15, 16, 17))

## fig 4

graph4aii <- plot_predictions(h4aii_model_action_support, condition = c("strong_rep", "align", "ad_align"), draw = FALSE)

select <- graph4aii %>% dplyr::select(estimate, conf.low, conf.high, strong_rep, align, ad_align)

ggplot(data = graph4aii, aes(x = align, y = estimate, shape = strong_rep)) +
  geom_point(position = position_dodge(width = 0.8), alpha = 0.5, size = 3) +
  geom_errorbar(
    aes(ymin = estimate - 1.96*std.error, ymax = estimate + 1.96*std.error),
    position = position_dodge(width = 0.8), color = "black", width = 0.1
  ) +
  theme_bw() +
  ylab("Support") +
  xlab("") +
  theme(
    legend.position = "bottom"
  ) + 
  scale_x_discrete(breaks = c(0, 1), labels = c("Not Co-Partisan\nNominee", "Co-Partisan\nNominee")) +
  facet_grid(~ ad_align, labeller = labeller(ad_align = ad_names)) +
  scale_shape_manual(name = "Partisanship", labels = c("Not Rep.", "Weak Rep.", "Strong Rep."), values = c(15, 16, 17))

# descriptives

ggplot(data = survey_clean, aes(x = feel)) +
  geom_histogram(binwidth = 5, fill = "gray", color = "black", size = 0.25) +
  labs(
    x = "Feelings of Support Towards Judicial Nominee",
    y = "Frequency") +
  theme_bw()

## faceted by nominee (fig 1)

ggplot(data = survey_clean2, aes(x = feel)) +
  geom_histogram(binwidth = 5, fill = "gray", color = "black", size = 0.25) +
  labs(
    x = "Feelings of Support Towards Judicial Nominee",
    y = "Frequency") +
  theme_bw() + facet_grid(~nominee)